package com.dreamlin.gankio.webview

import android.graphics.Bitmap
import android.os.Build
import android.view.View
import android.view.ViewGroup
import android.webkit.*
import com.dreamlin.gankio.R
import com.dreamlin.gankio.base.ui.NoVMActivity
import com.dreamlin.gankio.common.getVersionName
import kotlinx.android.synthetic.main.activity_webview.*
import kotlinx.android.synthetic.main.toolbar.*

class WebActivity : NoVMActivity() {

    override val layout: Int
        get() = R.layout.activity_webview
    lateinit var webClient: WebClient
    var title: String? = null
    var url: String? = null

    override fun initViews() {
        setDefaultWebSetting()
        title = intent.getStringExtra("TITLE")
        url = intent.getStringExtra("CONTENT")
        title?.let {
            tvTitle.text = it
        }
        url?.let {
            webView.loadUrl(it)
        }
    }

    private fun setDefaultWebSetting() {

        val settings = webView.settings

        //5.0以上开启混合加载
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
        }
        settings.loadWithOverviewMode = true
        settings.useWideViewPort = true
        //允许js代码
        settings.javaScriptEnabled = true
        //允许SessionStorage/LocalStorage存储
        settings.domStorageEnabled = true
        //禁用放缩
        settings.displayZoomControls = false
        settings.builtInZoomControls = false
        //禁用文字缩放
        settings.textZoom = 100
        //允许缓存，设置缓存位置
        settings.setAppCacheEnabled(true)
        settings.setAppCachePath(getDir("appcache", 0).path)
        //允许WebView使用File协议
        settings.allowFileAccess = true
        //不保存密码
        settings.savePassword = false
        //设置UA
        settings.userAgentString = settings.userAgentString + " gankio/" + getVersionName(this)
        //自动加载图片
        settings.loadsImagesAutomatically = true
        webClient = WebClient()
        webView.webViewClient = webClient
    }

    override fun onResume() {
        super.onResume()
        webView.onResume()
    }

    override fun onPause() {
        super.onPause()
        webView.onPause()
    }

    override fun onDestroy() {
        val viewGroup = webView.parent as ViewGroup
        viewGroup.removeView(viewGroup)
        webView.destroy()
        super.onDestroy()
    }

    override fun initData() {

    }


    override fun click(view: View) {

    }

    inner class WebClient : WebViewClient() {

        override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
            super.onPageStarted(view, url, favicon)
            showLoading("数据加载中...")
        }

        override fun onPageFinished(view: WebView?, url: String?) {
            super.onPageFinished(view, url)
            hideLoading()
        }

        override fun shouldInterceptRequest(
            view: WebView?,
            request: WebResourceRequest?
        ): WebResourceResponse? {
            return super.shouldInterceptRequest(view, request)
        }

        override fun onLoadResource(view: WebView?, url: String?) {
            super.onLoadResource(view, url)
        }
    }


}